<?php

namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\images\Images;
use cms\modules\v1\models\images\ImagesPosition;
use common\models\Functions;
use common\models\Status;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

class ImagesController extends DefaultController
{
    public $modelClass = 'cms\modules\v1\models\images\Images';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法

                ]
            ]
        ]);
    }

    //============================= 图片列表 =============================
    public function actionList($page = 1, $size = 50, $search = '') {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isGet) {
            return (new Images())->getListData($page, $size, $search);
        }
    }

    public function actionAddEdit($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        $model = new Images();
        if (Yii::$app->request->isGet) {
            $list =  $model->getInfo($id);
            return $list;
        }

        if (Yii::$app->request->isPost) {
            $model->setScenario('addEdit');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->addEdit();
            }
        }
    }

    public function actionPosition() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isGet) {
            return (new ImagesPosition())->getListForSelect();
        }
    }

    public function actionOrder() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (\Yii::$app->request->isPost) {
            return (new Images())->saveOrder();
        }
    }

    //切换启用禁用状态
    public function actionSwitchStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new Images())->switchStatus();
        }
    }

    public function actionJumpTypeMap() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return Functions::formatRestAPI(0, '',Status::JUMP_TARGET);
        }
    }
}
